package com.example.jacklee.justjava;

/**
 * Add your package below. Package name can be found in the project's AndroidManifest.xml file.
 * This is the package name our example uses:
 * <p>
 * package com.example.android.justjava;
 */

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import java.text.NumberFormat;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {

    int quantity;
    int price;
    int whippedCreamPrice, chocolatePrice = 0;
    boolean hasWhippedCream, hasChocolate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void buyMilktea(View view) {
        price = 12;
    }

    public void buyCappuccino(View view) {
        price = 15;
    }

    public void buyLatte(View view) {
        price = 20;
    }


    /**
     * This method is called when the order button is clicked.
     */
    public void checkWhippedCreamPrice(CheckBox checkBox) {
        if (checkBox.isChecked()) {
            whippedCreamPrice = 5;
        } else
            whippedCreamPrice = 0;
    }

    public void checkChocolatePrice(CheckBox checkBox) {
        if (checkBox.isChecked()) {
            chocolatePrice = 6;
        } else
            chocolatePrice = 0;
    }

    public void submitOrder(View view) {
        if (price == 0 || quantity == 0) {
            String errorMessage = "Sorry,sir. You haven't buy anything!\nPlease check it again!";
            displayMessage(errorMessage);
        } else {
            CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
            CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
            hasWhippedCream = whippedCreamCheckBox.isChecked();
            hasChocolate = chocolateCheckBox.isChecked();
            checkWhippedCreamPrice(whippedCreamCheckBox);
            checkChocolatePrice(chocolateCheckBox);
            createOrderSummary(price);
        }

    }

    public void displayMessage(String message) {
        TextView priceMessage = (TextView) findViewById(R.id.order_summary_text_view);
        priceMessage.setText(message);
    }

    /**
     * This method is called when the plus button is clikcd.
     */
    public void increment(View view) {
        if (quantity < 100) {
            quantity = quantity + 1;
            displayQuantity(quantity);
        }
        else
            Toast.makeText(this,"The Maximal is 100!", Toast.LENGTH_LONG).show();
    }

    /**
     * This method is called when the minus button is clicked.
     */
    public void decrement(View view) {
        if (quantity > 0) {
            quantity = quantity - 1;
            displayQuantity(quantity);
        }
        else
            Toast.makeText(this, "The Minimal quantity is 0!", Toast.LENGTH_LONG).show();
    }

    public void createOrderSummary(int PriceOfOrder) {

        EditText nameEditText = (EditText) findViewById(R.id.name_edit_text);
        String name = nameEditText.getText().toString();
        String message = "Name: " + name + "\n" +
                "Quantity: " + quantity + "\n" +
                "Total: " + "$ " + (quantity * PriceOfOrder + whippedCreamPrice + chocolatePrice) + "\n" +
                "Add whipped cream? " + hasWhippedCream + "\n" +
                "Add chocolate? : " + hasChocolate + "\n" +
                getString(R.string.thank_you);

        String [] addresses = new String[]{"Jack Lee's company"};
        String subject = "Erlich Bachman Coffee order for " + name;
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:")); // only email apps should handle this
        //intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, message);
        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
            Toast.makeText(MainActivity.this, "Order successfully!", Toast.LENGTH_SHORT).show();
        }
        //displayMessage(message);
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void displayQuantity(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);//对象转型
        quantityTextView.setText("" + number);
    }


}
